package edu.ynu.dao;
// default package

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import edu.ynu.po.Customer;
import edu.ynu.po.Enterprise;

/**
 	* A data access object (DAO) providing persistence and search support for Customer entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Customer
  * @author MyEclipse Persistence Tools 
 */

public class CustomerDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(CustomerDAO.class);
		//property constants
	public static final String CUSTNO = "custNo";
	public static final String NAME = "name";
	public static final String TYPE = "type";
	public static final String DEGREE = "degree";
	public static final String STAGE = "stage";
	public static final String CREDIT = "credit";
	public static final String SOURCE = "source";
	public static final String ISHOT = "ishot";
	public static final String CHARGE_MAN = "chargeMan";
	public static final String PHONE = "phone";
	public static final String EMAIL = "email";
	public static final String COUNTRY = "country";
	public static final String PROVINCE = "province";
	public static final String CITY = "city";
	public static final String ADDRESS = "address";
	public static final String CREATETIME = "createtime";
	public static final String LASTTIME = "lasttime";



    
    public void save(Customer transientInstance) {
        log.debug("saving Customer instance");
        try {
        	Session session = getSession();  
    		Transaction trans = session.beginTransaction();  
    		session.save(transientInstance);  
    		trans.commit();  
    		session.close(); 
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Customer persistentInstance) {
        log.debug("deleting Customer instance");
        try {
        	Session session = getSession();  
    		Transaction trans = session.beginTransaction();  
            session.delete(persistentInstance);
            trans.commit();  
    		session.close();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update (Customer cust) {
		Session session = getSession();  
		session.clear();
		Transaction trans = session.beginTransaction();  
		session.update(cust);  
		trans.commit();  
		session.close(); 
	}
	
	public void saveOrUpdate(Customer cust){
		Session session = getSession();  
		Transaction trans = session.beginTransaction();  
		session.saveOrUpdate(cust);  
		trans.commit();  
		session.close();  
	}
    
    public Customer findById( java.lang.Integer id) {
        log.debug("getting Customer instance with id: " + id);
        try {
            Customer instance = (Customer) getSession()
                    .get("edu.ynu.po.Customer", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Customer instance) {
        log.debug("finding Customer instance by example");
        try {
            List results = getSession()
                    .createCriteria("edu.ynu.po.Customer")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Customer instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Customer as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}
    
    public List findByCustNo( Object custNo){
    	return findByProperty(CUSTNO,custNo);
    }

	public List findByName(Object name
	) {
		return findByProperty(NAME, name
		);
	}
	
	public List findByType(Object type
	) {
		return findByProperty(TYPE, type
		);
	}
	
	public List findByDegree(Object degree
	) {
		return findByProperty(DEGREE, degree
		);
	}
	
	public List findByStage(Object stage
	) {
		return findByProperty(STAGE, stage
		);
	}
	
	public List findByCredit(Object credit
	) {
		return findByProperty(CREDIT, credit
		);
	}
	
	public List findBySource(Object source
	) {
		return findByProperty(SOURCE, source
		);
	}
	
	public List findByIshot(Object ishot
	) {
		return findByProperty(ISHOT, ishot
		);
	}
	
	public List findByChargeManId(Object chargeMan
	) {
		return findByProperty(CHARGE_MAN, chargeMan
		);
	}
	
	public List findByPhone(Object phone
	) {
		return findByProperty(PHONE, phone
		);
	}
	
	public List findByEmail(Object email
	) {
		return findByProperty(EMAIL, email
		);
	}
	
	public List findByCountry(Object country
	) {
		return findByProperty(COUNTRY, country
		);
	}
	
	public List findByProvince(Object province
	) {
		return findByProperty(PROVINCE, province
		);
	}
	
	public List findByCity(Object city
	) {
		return findByProperty(CITY, city
		);
	}
	
	public List findByAddress(Object address
	) {
		return findByProperty(ADDRESS, address
		);
	}
	
	public List findByCreatetime(Object createtime
	) {
		return findByProperty(CREATETIME, createtime
		);
	}
	
	public List findByLasttime(Object lasttime
	) {
		return findByProperty(LASTTIME, lasttime
		);
	}
	

	public List findAll(Map paramMap) {
		log.debug("finding all Customer instances");
		try {
			//String queryString = "from Customer";
	        //Query queryObject = getSession().createQuery(queryString);
			//return queryObject.list();
			//String start = (String) paramMap.get("start");
			//String limit = (String) paramMap.get("limit");
			String custNo = (String) paramMap.get("custNo");
			String mark =(String) paramMap.get("mark");
			String name = (String) paramMap.get("name");
			String chargeMan = (String) paramMap.get("chargeMan");
			String type = (String) paramMap.get("type");
			String city = (String) paramMap.get("city");
			Criteria c = getSession().createCriteria(Customer.class);
			// 条件查询
			if (StringUtils.isNotEmpty(custNo)) {
				c.add(Expression.like("custNo", "%" + custNo + "%"));// 编号
			}
			
			if (StringUtils.isNotEmpty(mark)) {
				c.add(Expression.like("mark", "%" + mark + "%"));// 标记
			}

			if (StringUtils.isNotEmpty(name)) {
				c.add(Expression.like("name", "%" + name + "%"));// 客户名称
			}
			if (StringUtils.isNotEmpty(chargeMan)) {
				c.add(Expression.like("chargeMan", "%"+chargeMan+"%"));
			}
			if (StringUtils.isNotEmpty(type)) {
				c.add(Expression.eq("type", type));// 客户等级
			}
			if (StringUtils.isNotEmpty(city)) {
				c.add(Expression.like("city", "%" + city + "%"));// 地区
			}
			List results = c.list();
			return results;
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
    public Customer merge(Customer detachedInstance) {
        log.debug("merging Customer instance");
        try {
            Customer result = (Customer) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Customer instance) {
        log.debug("attaching dirty Customer instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Customer instance) {
        log.debug("attaching clean Customer instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}